package pers.vic.system.system.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import pers.vic.base.bo.system.SysUser;
import pers.vic.base.contant.BaseConsoleConstant;
import pers.vic.base.controller.BaseConsoleController;
import pers.vic.base.pager.Lookup;
import pers.vic.base.pager.PageInfo;
import pers.vic.base.pager.Remind;
import pers.vic.system.system.lookup.SysUserLookup;
import pers.vic.system.system.service.SysRoleService;
import pers.vic.system.system.service.SysUserService;
import pers.vic.util.PasswordUtils;



/**
 * 系统用户表
 * 
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseConsoleController{
	

	@Resource
	private SysUserService sysUserService;
	
	@Resource
	private SysRoleService sysRoleService;
	
	
	//重定向到当前页面的page页
	private static final String REDIRECT_CURRENT_INDEX = "redirect:/system/user/";
	
	//JSP页面的页面前缀路径
	private static final String JSP_PATH = "system/user/";
	
	//绑定一些公共数据
	@ModelAttribute
	private void bindSelect(Model model) {
	
	}
	
	
    /**列表页*/
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String page(Model model) {
		PageInfo<SysUser> pager =  sysUserService.list(getLookup());
		model.addAttribute("pager",pager);
		return JSP_PATH + "list";
	}
	
	/**重写查询条件*/
	protected Lookup instanceLookup() {
		return new SysUserLookup();
	}
	
	/**查询*/
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String search(SysUserLookup lookup) {
		setLookup(lookup);
		return REDIRECT_CURRENT_INDEX;
	}
	
	/**前往新增页面*/
	@RequestMapping( value = "/0", method = RequestMethod.GET)
	public String add(Model model){
		return JSP_PATH + "form";
	}
	
	/**前往修改页面*/
	@RequestMapping( value = "/{id}", method = RequestMethod.GET)
	public String editor(@PathVariable int id, Model model){
		SysUser entity = sysUserService.findById(id);
		model.addAttribute("entity", entity);
		return JSP_PATH + "form";
	}
	
	/**保存*/
	@RequestMapping( value = "/{id}", method = RequestMethod.POST)
	public String save(@PathVariable int id, SysUser entity, RedirectAttributes attr){
		entity.setId(id);
		if(id == 0) {//to add  
			//默认密码为用户名
			entity.setPassword(PasswordUtils.entryptPassword(entity.getUsername()));
			entity.setCreateTime(new Date());
			sysUserService.insert(entity);
			attr.addFlashAttribute(Remind.success());
		}else if(id > 0 ){ //to update
			sysUserService.update(entity);
			attr.addFlashAttribute(Remind.success());
		}else {
			attr.addFlashAttribute(Remind.danger());
		}
		return REDIRECT_CURRENT_INDEX;
	}
	
	/**单个删除*/
	@RequestMapping( value = "/{id}/remove", method = RequestMethod.GET)
	public String delete(@PathVariable int id, RedirectAttributes attributes){
		sysUserService.delete(id);
		attributes.addFlashAttribute(Remind.success());
		return REDIRECT_CURRENT_INDEX;
	}
	
	/**批量删除*/
	@RequestMapping( value = "/remove", method = RequestMethod.POST)
	public String batchDelete(int[] ids, RedirectAttributes attributes){
		sysUserService.delete(ids);
		attributes.addFlashAttribute(Remind.success());
		return REDIRECT_CURRENT_INDEX;
	}
	
	/**修改用户状态 激活|冻结*/
	@RequestMapping( value = "/{id}/status", method = RequestMethod.GET)
	public String updateStatus(@PathVariable int id, @RequestParam int enabled, RedirectAttributes attributes){
		sysUserService.update(new SysUser(id, enabled));
		attributes.addFlashAttribute(Remind.success());
		return REDIRECT_CURRENT_INDEX;
	}
	
	
	/**管理员重置密码*/
	@RequestMapping( value = "/{id}/reset", method = RequestMethod.GET)
	public String reset(@PathVariable int id, RedirectAttributes attributes){
		SysUser user = sysUserService.findById(id);
		if(user == null) {
			attributes.addFlashAttribute(Remind.danger().setMessage("不存在的用户"));
		}else{
			sysUserService.update(new SysUser().setId(id).setPassword(PasswordUtils.entryptPassword(user.getUsername())));
			attributes.addFlashAttribute(Remind.success());
		}
		return REDIRECT_CURRENT_INDEX; 
	}
	
	
	/**前往修改密码页面*/
	@RequestMapping( value = "/updatePassword", method = RequestMethod.GET)
	public String password(Model model){
		return JSP_PATH + "password"; 
	}
	
	/**确认修改密码*/
	@RequestMapping( value = "/updatePassword", method = RequestMethod.POST)
	public String password(@RequestParam("old")String old, @RequestParam("password")String password, RedirectAttributes attributes){
		int id = getPrincipal().getUserId();
		SysUser user = sysUserService.findById(id);
		if(user == null) {
			attributes.addFlashAttribute(Remind.danger().setMessage("不存在的用户"));
		}else{
			old = DigestUtils.md5Hex(old);
			if(PasswordUtils.validatePassword(old, user.getPassword())) {
				attributes.addFlashAttribute(Remind.warning().setMessage("密码错误"));
			}else {
				sysUserService.update(new SysUser().setId(id).setPassword(PasswordUtils.entryptPassword(password)));
				attributes.addFlashAttribute(Remind.success());
			}
		}
		return "redirect:" + BaseConsoleConstant.CONSOLE_INDEX;
	}
	
	/**检测名称是否重复*/
	@RequestMapping( value = "/check")
	@ResponseBody
	public boolean checkName(Integer id, String username){
		boolean isOk = sysUserService.checkName(id,username);
		return isOk;
	}
	
	/**前往赋权页面*/
	@RequestMapping( value = "/{id}/role", method = RequestMethod.GET)
	public String role(@PathVariable int id, Model model){
		model.addAttribute("roles", sysRoleService.findUserRole(id));
		return JSP_PATH + "role"; 
	}
	/**
	 * 保存用户角色
	 */
	@RequestMapping( value = "/{id}/role", method = RequestMethod.POST)
	public String role(@PathVariable int id, @RequestParam int[] roles, RedirectAttributes attributes){
		attributes.addFlashAttribute(Remind.success());
		sysRoleService.updateUserRole(id, roles);
		return REDIRECT_CURRENT_INDEX; 
	}
	
	
}
